<?php
	header('Access-Control-Allow-Origin: *');  
	
	//////////////////////////////////////////////////////////
	// CAUTION: The Server ID Must be always in the data array
	$data["Server_ID"] = $_GET['ID'];
	//////////////////////////////////////////////////////////
	
	// CPU load
	$cpuLoad = "N/A";
	try
	{
		$cpuLoad = sys_getloadavg();
		$cpuLoad = $cpuLoad[0]."%";
	}
	catch(Exception $ex)
	{
	}
	$data["CPU Load"] = $cpuLoad;
	
	// Memory
	$totalRam = "0";
	$freeRam = "0";
	try
	{
		$memory = file('/proc/meminfo');
		foreach($memory as $line)
		{
		    $line = trim($line);
		    
		    if(preg_match('/^memtotal[^\d]+(\d+)[^\d]+$/i',$line,$m))
		    {
		       $totalRam = $m[1]/1024;
		    }
		    else if(preg_match('/^memfree[^\d]+(\d+)[^\d]+$/i',$line,$m))
		    {
		        $freeRam = $m[1]/1024;
		    }
		}
		
		$ramInUse = $totalRam - $freeRam;
		$ramUsagePercent = $ramInUse * 100 / $totalRam;
		$data["Ram Usage"] = number_format($ramUsagePercent,2) ."%";
		$data["Ram Use / Free"] = number_format($ramInUse,2). "MB / " . number_format($totalRam,2)."MB";
	}
	catch(Exception $ex)
	{
	}
	
	// CPU
	try
	{
		$cpu = file('/proc/cpuinfo');
	    $model = NULL;
	    $cores = 0;
	    foreach($cpu as $line)
	    {
	        if( preg_match( '/^model\s+name\s*:\s*(.+)$/i', $line, $m ) )
	        {
	            $model = $m[1];
	        }
	        else if( preg_match( '/^processor\s*:\s*\d+$/i', $line ) )
	        {
	            $cores++;
	        }
	    }
	    
	    $data['CPU'] = $model . " (".$cores." Cores)";
	    $data['Kernel'] = trim(file_get_contents("/proc/version"));
    }
    catch(Exception $ex)
    {
    }

	
	// Uptime
	try
	{
		$upTime = explode(" ", trim(file_get_contents("/proc/uptime")));
		$data['UpTime (Up / Idle)'] = $upTime[0]."/".$upTime[1];
	}
	catch(Exception $ex)
	{
	}
	
	// Hard disks (Mounts)
	$data["Hard Disks"] = array();
	try
	{
		foreach(file('/proc/mounts') as $hd)
		{
		    $hd = trim($hd);
		    if( $hd && $hd[0] == '/' )
		    {
		        $parts = explode( ' ', $hd );
		        if( $parts[0] != $parts[1] )
		        {
		            $device = $parts[0];
		            $folder = $parts[1];
		            $total  = disk_total_space($folder) / 1024;
		            $free   = disk_free_space($folder) / 1024;
		
		            if($total>0)
		            {
		                $used   = $total - $free;
		                $used_perc = ( $used * 100.0 ) / $total;
		
		                $data['Hard Disks'][] = array
		                (
		                    'dev' => $device,
		                    'total' => $total,
		                    'used' => $used,
		                    'free' => $free,
		                    'used_perc' => number_format($used_perc,2),
		                    'mount' => $folder
		                );
		            }
		        }
		    }
		}
	}
	catch(Exception $ex)
	{
		
	}
	
	// Network
	try
	{
		$ifname = NULL;
	
		if( file_exists('/etc/network/interfaces') )
		{
		    foreach( file('/etc/network/interfaces') as $line )
		    {
		        $line = trim($line);
		
		        if( preg_match( '/^iface\s+([^\s]+)\s+inet\s+.+$/', $line, $m ) && $m[1] != 'lo' )
		        {
		            $ifname = $m[1];
		            break;
		        }
		    }
		}
		else
		{
		    foreach( glob('/sys/class/net/*') as $filename )
		    {
		        if( $filename != '/sys/class/net/lo' && file_exists( "$filename/statistics/rx_bytes" ) && trim( file_get_contents("$filename/statistics/rx_bytes") ) != '0' )
				{
					$parts = explode( '/', $filename );
		            $ifname = array_pop( $parts );
		        }
		    }
		}
		
		if( $ifname != NULL )
		{
		    $data['Network Send / Receive'] = number_format(trim(file_get_contents("/sys/class/net/$ifname/statistics/tx_bytes"))/1024,0) ." / ". number_format(trim(file_get_contents("/sys/class/net/$ifname/statistics/rx_bytes"))/1024,0) ;
		   
		}
	}
	catch(Exception $ex)
	{
	
	}


	// Export data to JSON Format
	print json_encode($data);
?>
